<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="displayModuleTitle"
    :footer="null"
    width="850px"
  >
    <BasicTable
      :data-source="tableData"
      @register="registerTable"
      :pagination="false"
      :scroll="{ x: false, y: '300px' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAddCustomizeGroupButtonClick">新增</a-button>
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:carry-out-outlined',
              tooltip: '完成',
              color: 'success',
              ifShow: taskInfo.hasdispatch == true && taskInfo.hasfinish == false,
              popConfirm: {
                title: '是否确认完成任务分组',
                placement: 'left',
                confirm: onFinishTaskGroupButtonClick.bind(null, record),
              },
            },
            {
              icon: 'ant-design:close-circle-outlined',
              color: 'error',
              tooltip: '终止',
              ifShow: taskInfo.hasdispatch == true && taskInfo.hasfinish == false,
              popConfirm: {
                title: '是否确认终止任务分组',
                placement: 'left',
                confirm: onEndTaskGroupButtonClick.bind(null, record),
              },
            },
            {
              tooltip: '继续',
              color: 'success',
              ifShow: taskInfo.hasdispatch == true && taskInfo.hasfinish == false,
              popConfirm: {
                title: '是否确认继续任务分组',
                placement: 'left',
                confirm: onContinueTaskGroupButtonClick.bind(null, record),
              },
            },
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditCustomizeGroupButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteCustomizeGroupButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateTaskGroup
      :orgcode="orgcode"
      @register="registerAddOrUpdateTaskGroupModal"
      @success="afterSuccess"
    />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { queryEnumOptions } from '/@/api/platform/enum';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';
  import AddOrUpdateTaskGroup from './AddOrUpdateTaskGroup.vue';
  import { getTaskGroups, deleteGroup } from '/@/api/ehs/taskGroup';
  import { finishGroup, endGroup, continueGroup } from '/@/api/ehs/taskGroup';
  import { customizeGroupTableColumns } from './taskConsole.data';
  const { createMessage } = useMessage();
  export default defineComponent({
    components: {
      BasicModal,
      BasicTable,
      TableAction,
      AddOrUpdateTaskGroup,
    },
    name: 'CustomizeGroupManage',
    props: ['orgcode'],
    setup(props, { emit }) {
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      // 任务id
      const dataId = ref();
      const tableData = ref([]);
      const taskInfo = ref({});
      const [registerModal, { closeModal }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.dataId) {
            dataId.value = unrefData.dataId;
            taskInfo.value = unrefData.taskInfo;
            getTableDataValue();
          }
        }
      });
      const [registerAddOrUpdateTaskGroupModal, { openModal: openAddOrUpdateTaskGroupModal }] =
        useModal();
      const [registerTable, { getSelectRowKeys }] = useTable({
        columns: customizeGroupTableColumns,
        canResize: true,
        striped: true,
        showTableSetting: true,
        showIndexColumn: true,
        isTreeTable: false,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        // rowKey: 'groupid',
        // rowSelection: {
        //   type: 'checkbox',
        // },
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      function getTableDataValue() {
        getTaskGroups(dataId.value).then((res) => {
          if (res) tableData.value = res;
          else tableData.value = [];
        });
      }

      /**
       * 打开新增窗口
       */

      function onAddCustomizeGroupButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '自定义分组管理',
          operateType: OperationTypeEnum.ADD,
          taskId: dataId.value,
        };
        openAddOrUpdateTaskGroupModal(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditCustomizeGroupButtonClick(record: EhsCustomizeGroupDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '自定义分组管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.groupid,
          taskId: dataId.value,
        };
        openAddOrUpdateTaskGroupModal(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteCustomizeGroupButtonClick(record: EhsCustomizeGroupDto) {
        deleteGroup(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 完成任务分组
       */
      function onFinishTaskGroupButtonClick(record) {
        finishGroup(record.groupid).then((res) => {
          if (!res) {
            createMessage.success('任务分组完成成功');
            afterSuccess();
          } else {
            createMessage.error('任务分组完成失败');
          }
        });
      }

      /**
       * 终止任务分组
       */
      function onEndTaskGroupButtonClick(record) {
        endGroup(record.groupid).then((res) => {
          if (!res) {
            createMessage.success('任务分组终止成功');
            afterSuccess();
          } else {
            createMessage.error('任务分组终止失败');
          }
        });
      }

      /**
       * 继续任务分组
       */
      function onContinueTaskGroupButtonClick(record) {
        continueGroup(record.groupid).then((res) => {
          if (!res) {
            createMessage.success('任务分组继续成功');
            afterSuccess();
          } else {
            createMessage.error('任务分组继续失败');
          }
        });
      }

      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        getTableDataValue();
      }
      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      onMounted(() => {});
      return {
        registerModal,
        registerTable,
        registerAddOrUpdateTaskGroupModal,
        closeModal,
        afterSuccess,
        tableData,
        taskInfo,
        displayModuleTitle,
        onAddCustomizeGroupButtonClick,
        onEditCustomizeGroupButtonClick,
        onDeleteCustomizeGroupButtonClick,
        onFinishTaskGroupButtonClick,
        onEndTaskGroupButtonClick,
        onContinueTaskGroupButtonClick,
      };
    },
  });
</script>
